from multiprocessing import Process
from multiprocessing import Lock
from multiprocessing import Queue
import time
import json
import os
import random
# 多进程原则上是不能互相通信的,它们在内存级别数据隔离的.不代表磁盘上数据隔离.
# 它们可以共同操作一个文件.

def search(ticket):
    time.sleep(random.random())
    print(f'剩余票数{ticket}')


def _get(q):
    dic = q.get()
    time.sleep(random.randint(1,3))
    if dic['count'] > 0:
        dic['count'] -= 1
        print(f'{os.getpid()}用户购买成功')
    else:
        print('没票了.....')
    q.put(dic)

def task(ticket,q):
    search(ticket)
    _get(q)


if __name__ == '__main__':
    q = Queue(1)
    q.put({'count': 1})
    ticket = 1
    for i in range(5):
        p = Process(target=task,args=(ticket,q))
        p.start()